package mmc.math.spline;

public class BezierSpline implements Spline{

	private double s0, s1, p0, c0, c1, p1;
	
	public BezierSpline(double s0, double s1, double p0, double c0, double c1, double p1) {
		this.p0 = p0;
		this.c0 = c0;
		this.c1 = c1;
		this.p1 = p1;
		this.s0 = s0;
		this.s1 = s1-s0;
	}

	@Override
	public double evaluateAt(double x) {
		x -= s0;
		x /= s1;
		
		if(x<0||x>1)
			throw new RuntimeException("invalid x: "+x);
		
		double x2 = x*x,
		       x3 = x2*x;
		
		return p0 * (1-3*x+3*x2-x3) + c0 * (3*x - 6*x2 +3*x3) + c1 * (3*x2 - 3*x3) + p1 * x3;
	}

}
